package com.cheetah.start.module.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.cheetah.start.common.annotation.Log;
import com.cheetah.start.common.base.result.Result;
import com.cheetah.start.common.base.result.ResultController;
import com.cheetah.start.common.base.result.ResultTree;
import com.cheetah.start.common.enums.BusinessTypeEnum;
import com.cheetah.start.common.enums.LogTypeEnum;
import com.cheetah.start.module.system.domain.SysMenu;
import com.cheetah.start.module.system.domain.SysRealMenu;
import com.cheetah.start.module.system.service.SysMenuService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.constraints.NotNull;
import java.util.List;


/**
 * 系统菜单控制器
 *
 * @author Feel
 * @date 2021/08/22
 */
@Api(tags = "菜单管理")
@Slf4j
@Validated
@RestController
@RequestMapping("/system/menu")
public class SysMenuController extends ResultController {
    @Autowired
    private SysMenuService sysMenuService;
    @Value("${server.servlet.context-path}")
    private String suffix;

    @GetMapping("/add")
    public ModelAndView add(Model model) {
        return jumpPage("/system/menu/add");
    }

    /**
     * 编辑页面
     *
     * @param model  模型
     * @param menuId 菜单id
     * @return {@link ModelAndView}
     */
    @GetMapping("/edit")
    public ModelAndView edit(Model model, Integer menuId) {
        SysMenu menu = sysMenuService.getById(menuId);
        model.addAttribute("sysMenu", menu);
        return jumpPage("/system/menu/edit");
    }

    /**
     * 用户菜单
     *
     * @return {@link SysRealMenu}
     */
    @GetMapping("/userMenu")
    public List<SysRealMenu> userMenu() {
        int userId = StpUtil.getLoginIdAsInt();
        List<SysRealMenu> sysRealMenus = sysMenuService.menuList(userId);
        sysMenuService.setPreFix(sysRealMenus, suffix);
        return sysMenuService.toMenu(sysRealMenus, "0");
    }


    /**
     * 父
     *
     * @return {@link ResultTree}
     */
    @GetMapping("/selectParent")
    public ResultTree parent() {
        List<SysMenu> list = sysMenuService.list();
        SysMenu sysMenu = new SysMenu();
        sysMenu.setMenuName("顶级权限").setMenuId(0).setParentId(-1);
        list.add(sysMenu);
        return dataTree(list);
    }


    /**
     * 禁用
     *
     * @param menuId 菜单id
     * @return {@link Result}
     */
    @SaCheckPermission(value = "sys:menu:switch", orRole = "admin")
    @Log(title = "权限", describe = "禁用", type = BusinessTypeEnum.EDIT, logType = LogTypeEnum.OPERATE)
    @PutMapping("/disable")
    public Result disable(@NotNull(message = "ID不能为空") Integer menuId) {
        return sysMenuService.enableOrDisable(menuId, "0");
    }

    /**
     * 启用
     *
     * @param menuId 菜单id
     * @return {@link Result}
     */
    @SaCheckPermission(value = "sys:menu:switch", orRole = "admin")
    @PutMapping("/enable")
    @Log(title = "权限", describe = "启用", type = BusinessTypeEnum.EDIT, logType = LogTypeEnum.OPERATE)
    public Result enable(@NotNull(message = "ID不能为空") Integer menuId) {
        return sysMenuService.enableOrDisable(menuId, "1");
    }


}
